package com.zzyq.pc.mongodb.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.aggregation.Accumulator;
import org.mongodb.morphia.aggregation.AggregationPipeline;
import org.mongodb.morphia.aggregation.Group;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.Sort;

import com.mongodb.AggregationOptions;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.QueryOperators;
import com.zzyq.pc.common.plugin.db.PageView;
import com.zzyq.pc.mongodb.model.MongodbDate;
import com.zzyq.pc.mongodb.model.MongodbPoint;
import com.zzyq.pc.utils.MongoHelper;
import com.zzyq.pc.web.configure.base.Menu;

public class MongodbWarning {

	MongoHelper mongodb = MongoHelper.getInstance();
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

	/**
	 * 舆情预警——首页
	 * 
	 * @param MongodbDate
	 *            对象名
	 * @param ifmIds
	 *            方案ID集合
	 * @return
	 */
	public List<MongodbDate> findWarning(Class<?> MongodbDate,
			List<String> ifmIds, Integer greaterForward,Date timeScope) {
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		//List<String> typelist = new ArrayList<String>();
		/*typelist.add("WeiXin");
		typelist.add("Micro");
		typelist.add("BBS");*/
		Calendar lastdate = Calendar.getInstance();
		lastdate.add(Calendar.DATE, -6);// 获取七天前的时间
		Query<?> query = mongodb.getDatastore().createQuery(MongodbDate);
		query.field("ifmId").in(ifmIds);
		//query.field("articleType").in(typelist);
		timeScope = timeScope != null ? timeScope : new Date(lastdate.getTime().getTime());
		query.field("releaseTime").greaterThanOrEq(timeScope);
		AggregationPipeline pipeline = mongodb.getDatastore()
				.createAggregation(MongodbDate).match(query)
				.group(Group.id(Group.grouping("groupId")),
						Group.grouping("groupId", Group.first("groupId")),
						Group.grouping("ifmId", Group.first("ifmId")),
						Group.grouping("ifmName", Group.first("ifmName")),
						Group.grouping("articleId", Group.first("articleId")),
						Group.grouping("articleType",Group.first("articleType")),
						Group.grouping("title", Group.first("title")),
						Group.grouping("url", Group.first("url")),
						Group.grouping("author", Group.first("author")),
						Group.grouping("releaseTime",Group.first("releaseTime")),
						Group.grouping("weiboSourceContent",Group.first("weiboSourceContent")),
						Group.grouping("weiboContent",Group.first("weiboContent")),
						Group.grouping("weiboBlogger",Group.first("weiboBlogger")),
						Group.grouping("websiteName",Group.first("websiteName")),
						Group.grouping("timestamp", Group.first("timestamp")),
						Group.grouping("reprintNumb",new Accumulator("$sum", 1)),
						Group.grouping("sort_timestamp",Group.first("sort_timestamp")),
						Group.grouping("original", Group.first("original")))
				.sort(Sort.descending("reprintNumb")).limit(5);
		Iterator<?> iterator = pipeline.aggregate(MongodbDate,
				AggregationOptions.builder().allowDiskUse(true).build());
		while (iterator.hasNext()) {
			MongodbDate date = (MongodbDate) iterator.next();
			if (date.getReprintNumb() > greaterForward) {
				list.add(date);
			}
		}
		return list;
	}

	/**
	 * 高危舆情——首页
	 * 
	 * @param MongodbDate
	 *            对象名
	 * @param ifmIds
	 *            方案ID集合
	 * @return
	 */
	/*public List<MongodbDate> findRisk(Class<?> entity, List<String> ifmIds,
			Integer greaterForward, Integer lessForward) {
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		Calendar lastdate = Calendar.getInstance();
		lastdate.add(Calendar.DATE, -7);
		Query<?> query = mongodb.getDatastore().createQuery(MongodbPoint.class);
		query.and(
				query.criteria("ifmId").in(ifmIds),
				query.criteria("timestamp").greaterThan(
						lastdate.getTime().getTime()), query.or(
						query.criteria("reprintNumb").greaterThanOrEq(
								greaterForward), query.criteria("reprintNumb")
								.lessThan(lessForward)),
				query.criteria("articleType").equal("News"));
		Iterator<?> tor = query.order(Sort.descending("timestamp")).limit(5)
				.iterator();
		while (tor.hasNext()) {
			MongodbPoint point = (MongodbPoint) tor.next();
			// 根据主键ID查询数据 
			Query<?> query2 = mongodb.getDatastore().createQuery(entity);
			Iterator<?> tor2 = query2.field("id").equal(point.getId())
					.iterator();
			while (tor2.hasNext()) {
				MongodbDate date = (MongodbDate) tor2.next();
				date.setContent("");
				date.setReprintNumb(point.getReprintNumb());
				list.add(date);
			}
		}
		return list;
	}*/

	/**
	 * 高危舆情——首页
	 * 
	 * @param MongodbDate
	 *            对象名
	 * @param ifmIds
	 *            方案ID集合
	 * @return
	 */
	public List<MongodbDate> findRisk(Class<?> entity, List<String> ifmIds,
			Integer greaterForward, Integer lessForward,Date timeScope) {
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		Calendar lastdate = Calendar.getInstance();
		lastdate.add(Calendar.DATE, -6);// 获取七天前的时间
		Query<?> query = mongodb.getDatastore().createQuery(entity);
		query.field("ifmId").in(ifmIds);
		timeScope = null != timeScope ? timeScope : new Date(lastdate.getTime().getTime());
		query.field("releaseTime").greaterThanOrEq(timeScope);
		AggregationPipeline pipeline = mongodb.getDatastore()
				.createAggregation(entity).match(query)
				.group(Group.id(Group.grouping("groupId")),
						Group.grouping("groupId", Group.first("groupId")),
						Group.grouping("ifmId", Group.first("ifmId")),
						Group.grouping("ifmName", Group.first("ifmName")),
						Group.grouping("articleId", Group.first("articleId")),
						Group.grouping("articleType",Group.first("articleType")),
						Group.grouping("title", Group.first("title")),
						Group.grouping("url", Group.first("url")),
						Group.grouping("author", Group.first("author")),
						Group.grouping("releaseTime",Group.first("releaseTime")),
						Group.grouping("weiboSourceContent",Group.first("weiboSourceContent")),
						Group.grouping("weiboContent",Group.first("weiboContent")),
						Group.grouping("weiboBlogger",Group.first("weiboBlogger")),
						Group.grouping("websiteName",Group.first("websiteName")),
						Group.grouping("original", Group.first("original")),
						Group.grouping("timestamp", Group.first("timestamp")),
						Group.grouping("reprintNumb",new Accumulator("$sum", 1)),
						Group.grouping("sort_timestamp",Group.first("sort_timestamp")),
						Group.grouping("original", Group.first("original")))
				.sort(Sort.descending("reprintNumb")).limit(1000);
		Iterator<?> iterator = pipeline.aggregate(entity, AggregationOptions
				.builder().allowDiskUse(true).build());
		while (iterator.hasNext()) {
			MongodbDate date = (MongodbDate) iterator.next();
			date.setContent(null);
			if (date.getArticleType().equals(Menu.News.getName()) && date.getReprintNumb() < lessForward) {
				list.add(date);
			} else if(date.getReprintNumb() >= greaterForward && date.getReprintNumb() < lessForward) {
				list.add(date);
			}
			if (list.size() == 5) {
				return list;
			}
		}
		return list;
	}

	/**
	 * 负面舆情——首页
	 * 
	 * @param MongoDate
	 *            对象名
	 * @param ifmIds
	 *            方案ID集合
	 * @return
	 */
	public List<MongodbDate> findNegative(Class<?> entity, List<String> ifmIds,
			Integer lessForward,Date timeScope) {
		List<String> typelist = new ArrayList<String>();
		typelist.add("WeiXin");
		typelist.add("Micro");
		typelist.add("BBS");
		Calendar lastdate = Calendar.getInstance();
		lastdate.add(Calendar.DATE, -6);// 过去七天
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		Query<?> query = mongodb.getDatastore().createQuery(entity);
		query.field("ifmId").in(ifmIds);
		query.field("articleType").in(typelist);
		timeScope = null != timeScope ? timeScope : new Date(lastdate.getTime().getTime());
		query.field("releaseTime").greaterThanOrEq(timeScope);
		AggregationPipeline pipeline = mongodb.getDatastore()
				.createAggregation(entity).match(query)
				.group(Group.id(Group.grouping("groupId")),
						Group.grouping("groupId", Group.first("groupId")),
						Group.grouping("ifmId", Group.first("ifmId")),
						Group.grouping("ifmName", Group.first("ifmName")),
						Group.grouping("articleId", Group.first("articleId")),
						Group.grouping("articleType",Group.first("articleType")),
						Group.grouping("title", Group.first("title")),
						Group.grouping("url", Group.first("url")),
						Group.grouping("author", Group.first("author")),
						Group.grouping("releaseTime",Group.first("releaseTime")),
						Group.grouping("weiboSourceContent",Group.first("weiboSourceContent")),
						Group.grouping("weiboContent",Group.first("weiboContent")),
						Group.grouping("weiboBlogger",Group.first("weiboBlogger")),
						Group.grouping("websiteName",Group.first("websiteName")),
						Group.grouping("timestamp", Group.first("timestamp")),
						Group.grouping("reprintNumb",new Accumulator("$sum", 1)),
						Group.grouping("sort_timestamp",Group.first("sort_timestamp")),
						Group.grouping("original", Group.first("original")))
				.sort(Sort.ascending("reprintNumb"))
				.sort(Sort.descending("sort_timestamp"))
				.sort(Sort.descending("releaseTime")).limit(5)
				.sort(Sort.descending("reprintNumb"));
		Iterator<?> iterator = pipeline.aggregate(entity, AggregationOptions
				.builder().allowDiskUse(true).build());
		while (iterator.hasNext()) {
			MongodbDate date = (MongodbDate) iterator.next();
			if (date.getReprintNumb() < lessForward) {
				list.add(date);
			}
		}
		return list;
	}

	/**
	 * 最新舆情——首页
	 * 
	 * @param MongoDate
	 *            对象
	 * @param ifmIds
	 *            方案ID集合
	 */
	public List<MongodbDate> findNewest(Class<?> entity, List<String> ifmIds,Date timeScope) {
		Calendar lastdate = Calendar.getInstance();
		lastdate.add(Calendar.DATE, -6);
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		Query<?> query = mongodb.getDatastore().createQuery(entity);
		query.field("ifmId").in(ifmIds);
		timeScope = null != timeScope ? timeScope : new Date(lastdate.getTime().getTime());
		query.field("releaseTime").greaterThanOrEq(timeScope);
		AggregationPipeline pipeline = mongodb.getDatastore().createAggregation(entity).match(query)
				.group(Group.id(Group.grouping("groupId")),
						Group.grouping("groupId", Group.first("groupId")),
						Group.grouping("ifmId", Group.first("ifmId")),
						Group.grouping("ifmName", Group.first("ifmName")),
						Group.grouping("articleId", Group.first("articleId")),
						Group.grouping("articleType",Group.first("articleType")),
						Group.grouping("title", Group.first("title")),
						Group.grouping("url", Group.first("url")),
						Group.grouping("author", Group.first("author")),
						Group.grouping("releaseTime",Group.first("releaseTime")),
						Group.grouping("weiboSourceContent",Group.first("weiboSourceContent")),
						Group.grouping("weiboContent",Group.first("weiboContent")),
						Group.grouping("weiboBlogger",Group.first("weiboBlogger")),
						Group.grouping("websiteName",Group.first("websiteName")),
						Group.grouping("original", Group.first("original")),
						Group.grouping("timestamp", Group.first("timestamp")),
						Group.grouping("sort_timestamp",Group.first("sort_timestamp")),
						Group.grouping("reprintNumb",new Accumulator("$sum", 1)))
				.sort(Sort.descending("sort_timestamp"))
				.sort(Sort.descending("releaseTime")).limit(5);
		Iterator<?> iterator = pipeline.aggregate(entity, AggregationOptions
				.builder().allowDiskUse(true).build());
		while (iterator.hasNext()) {
			MongodbDate date = (MongodbDate) iterator.next();
			list.add(date);
		}
		return list;
	}

	/**
	 * 舆情预警——更多
	 * 
	 * @param MongodbDate
	 *            对象名
	 * @param ifmIds
	 *            方案ID集合
	 * @param pageView
	 *            分页参数
	 * @return
	 */
	public List<MongodbDate> findWarningMore(Class<?> entity,
			List<String> ifmIds, PageView pageView, Integer greaterForward) {
		int pageIndex = pageView.getPageNow();
		int pageSize = pageView.getPageSize();
		List<MongodbDate> list = new ArrayList<MongodbDate>();
	//	List<String> typelist = new ArrayList<String>();
		/*typelist.add("WeiXin");
		typelist.add("Micro");
		typelist.add("BBS");*/
		Query<?> query = mongodb.getDatastore().createQuery(entity);
		query.field("ifmId").in(ifmIds);
		//query.field("articleType").in(typelist);
		// 聚合
		AggregationPipeline pipeline = mongodb.getDatastore()
				.createAggregation(entity).match(query)
				.sort(Sort.ascending("sort_timestamp")).sort(Sort.ascending("releaseTime"))
				.group(Group.id(Group.grouping("groupId")),
						Group.grouping("groupId", Group.first("groupId")),
						Group.grouping("ifmId", Group.first("ifmId")),
						Group.grouping("ifmName", Group.first("ifmName")),
						Group.grouping("articleId", Group.first("articleId")),
						Group.grouping("articleType",Group.first("articleType")),
						Group.grouping("title", Group.first("title")),
						Group.grouping("url", Group.first("url")),
						Group.grouping("author", Group.first("author")),
						Group.grouping("releaseTime",Group.first("releaseTime")),
						Group.grouping("weiboSourceContent",Group.first("weiboSourceContent")),
						Group.grouping("weiboContent",Group.first("weiboContent")),
						Group.grouping("weiboBlogger",Group.first("weiboBlogger")),
						Group.grouping("websiteName",Group.first("websiteName")),
						Group.grouping("original", Group.first("original")),
						Group.grouping("timestamp", Group.first("timestamp")),
						Group.grouping("sort_timestamp",Group.first("sort_timestamp")),
						Group.grouping("reprintNumb",new Accumulator("$sum", 1)))
				.sort(Sort.descending("reprintNumb"))
				.skip(pageIndex * pageSize - pageSize).limit(pageSize);
		Iterator<?> iterator = pipeline.aggregate(entity, AggregationOptions
				.builder().allowDiskUse(true).build());
		while (iterator.hasNext()) {
			MongodbDate date = (MongodbDate) iterator.next();
			date.setContent(null);
			// 判断转载数，后期根据需求肯能需要更改
			if (date.getReprintNumb() > greaterForward) {
				list.add(date);
			}
		}
		return list;
	}

	/**
	 * 负面舆情——更多
	 * 
	 * @param MongoDate
	 *            对象名
	 * @param ifmIds
	 *            方案ID集合
	 * @param pageView
	 *            分页参数
	 * @return
	 */
	public List<MongodbDate> findNegativeMore(Class<?> entity,
			List<String> ifmIds, PageView pageView, Integer lessForward) {
		int pageIndex = pageView.getPageNow();
		int pageSize = pageView.getPageSize();
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		List<String> typelist = new ArrayList<String>();
		typelist.add(Menu.WeiXin.name());
		typelist.add(Menu.Micro.name());
		typelist.add(Menu.BBS.name());
		Query<?> query = mongodb.getDatastore().createQuery(entity);
		query.field("ifmId").in(ifmIds);
		query.field("articleType").in(typelist);
		// 聚合
		AggregationPipeline pipeline = mongodb.getDatastore()
				.createAggregation(entity).match(query)
				.sort(Sort.ascending("sort_timestamp")).sort(Sort.ascending("releaseTime"))
				.group(Group.id(Group.grouping("groupId")),
						Group.grouping("groupId", Group.first("groupId")),
						Group.grouping("ifmId", Group.first("ifmId")),
						Group.grouping("ifmName", Group.first("ifmName")),
						Group.grouping("articleId", Group.first("articleId")),
						Group.grouping("articleType",Group.first("articleType")),
						Group.grouping("title", Group.first("title")),
						Group.grouping("url", Group.first("url")),
						Group.grouping("author", Group.first("author")),
						Group.grouping("releaseTime",Group.first("releaseTime")),
						Group.grouping("weiboSourceContent",Group.first("weiboSourceContent")),
						Group.grouping("weiboContent",Group.first("weiboContent")),
						Group.grouping("weiboBlogger",Group.first("weiboBlogger")),
						Group.grouping("websiteName",Group.first("websiteName")),
						Group.grouping("original", Group.first("original")),
						Group.grouping("timestamp", Group.first("timestamp")),
						Group.grouping("sort_timestamp",Group.first("sort_timestamp")),
						Group.grouping("reprintNumb",new Accumulator("$sum", 1)))
				.sort(Sort.ascending("reprintNumb")).sort(Sort.descending("sort_timestamp"))
				.sort(Sort.descending("releaseTime"))
				.skip(pageIndex * pageSize - pageSize).limit(pageSize)
				.sort(Sort.descending("reprintNumb"));
		Iterator<?> iterator = pipeline.aggregate(entity, AggregationOptions
				.builder().allowDiskUse(true).build());
		while (iterator.hasNext()) {
			MongodbDate date = (MongodbDate) iterator.next();
			// 判断转载数，后期根据需求肯能需要更改
			if (date.getReprintNumb() < lessForward) {
				list.add(date);
			}
		}
		return list;
	}

	/**
	 * 最新舆情——更多
	 * 
	 * @param MongoDate
	 * @param pageView
	 * @param ifmIds
	 * @return
	 */
	public List<MongodbDate> findNewestMore(Class<?> entity, PageView pageView,
			List<String> ifmIds) {
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		int pageIndex = pageView.getPageNow();
		int pageSize = pageView.getPageSize();
		Query<?> query = mongodb.getDatastore().createQuery(entity);
		query.field("ifmId").in(ifmIds);
		// 聚合
		AggregationPipeline pipeline = mongodb.getDatastore()
				.createAggregation(entity).match(query)
				.sort(Sort.ascending("sort_timestamp")).sort(Sort.ascending("releaseTime"))
				.group(Group.id(Group.grouping("groupId")),
						Group.grouping("groupId", Group.first("groupId")),
						Group.grouping("ifmId", Group.first("ifmId")),
						Group.grouping("ifmName", Group.first("ifmName")),
						Group.grouping("articleId", Group.first("articleId")),
						Group.grouping("articleType",Group.first("articleType")),
						Group.grouping("title", Group.first("title")),
						Group.grouping("url", Group.first("url")),
						Group.grouping("author", Group.first("author")),
						Group.grouping("releaseTime",Group.first("releaseTime")),
						Group.grouping("weiboSourceContent",Group.first("weiboSourceContent")),
						Group.grouping("weiboContent",Group.first("weiboContent")),
						Group.grouping("weiboBlogger",Group.first("weiboBlogger")),
						Group.grouping("websiteName",Group.first("websiteName")),
						Group.grouping("original", Group.first("original")),
						Group.grouping("timestamp", Group.first("timestamp")),
						Group.grouping("sort_timestamp",Group.first("sort_timestamp")),
						Group.grouping("reprintNumb",new Accumulator("$sum", 1)))
				.sort(Sort.descending("sort_timestamp")).sort(Sort.descending("releaseTime"))
				.skip(pageIndex * pageSize - pageSize).limit(pageSize);
		Iterator<?> iterator = pipeline.aggregate(entity, AggregationOptions
				.builder().allowDiskUse(true).build());
		while (iterator.hasNext()) {
			MongodbDate date = (MongodbDate) iterator.next();
			date.setContent(null);
			list.add(date);
		}
		return list;
	}

	/**
	 * 高危舆情——更多
	 * 
	 * @param MongoDate
	 * @param pageView
	 * @param ifmIds
	 * @return
	 */
	public List<MongodbDate> findHighRiskMore(Class<?> entity,
			PageView pageView, List<String> ifmIds, Integer greaterForward,
			Integer lessForward) {
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		int pageIndex = pageView.getPageNow();
		int pageSize = pageView.getPageSize();
		DBCollection tion = mongodb.getDatastore().getCollection(MongodbPoint.class);
		// 条件
		BasicDBObject query = new BasicDBObject();
		query.append(QueryOperators.AND,
				new BasicDBObject[] { new BasicDBObject("ifmId",
						new BasicDBObject("$in", ifmIds)) });
		/*query.append(QueryOperators.OR, new BasicDBObject[] {
				new BasicDBObject("reprintNumb", new BasicDBObject("$lt",
						lessForward).append("$gte", greaterForward)),
						new BasicDBObject("articleType","News")});*/
		query.append(QueryOperators.OR, new BasicDBObject[] {
				new BasicDBObject("reprintNumb", new BasicDBObject("$lt",
						lessForward).append("$gte", greaterForward)),
						new BasicDBObject("articleType","News").append("reprintNumb", new BasicDBObject("$lt",lessForward))});
		// 排序分页查询
		DBCursor sor = tion.find(query)
				.sort(new BasicDBObject("timestamp", -1))
				.skip(pageIndex * pageSize - pageSize).limit(pageSize);
		while (sor.hasNext()) {
			DBObject object = sor.next();
			MongodbPoint point = new MongodbPoint();
			//point.setId(String.valueOf(object.get("_id")));
			point.setId(String.valueOf(object.get("earliestId")));
			point.setReprintNumb(Integer.valueOf(String.valueOf(object.get("reprintNumb"))));
			// 根据循环的10个ID查询数据具体项
			Query<?> query2 = mongodb.getDatastore().createQuery(entity);
			Iterator<?> tor = query2.field("id").equal(point.getId()).iterator();
			while (tor.hasNext()) {
				MongodbDate date = (MongodbDate) tor.next();
				date.setContent(null);
				date.setReprintNumb(point.getReprintNumb());
				list.add(date);
			}
		}
		return list;
	}

	/**
	 * 高危舆情——更多
	 * 
	 * @param MongoDate
	 * @param pageView
	 * @param ifmIds
	 * @return
	 */

	/*public List<MongodbDate> findHighRiskMore(Class<?> entity,
			PageView pageView, List<String> ifmIds, Integer greaterForward,
			Integer lessForward) {
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		int pageIndex = pageView.getPageNow(); // 当前页码 int pageSize =
		pageView.getPageSize(); // 当前页显示多少条 Query<?> query =
		mongodb.getDatastore().createQuery(entity);
		query.field("ifmId").in(ifmIds);

		List<MongodbDate> datelist = new ArrayList<MongodbDate>();
		Query<?> query2 = mongodb.getDatastore().createQuery(entity);
		List<String> typelist = new ArrayList<String>();
		typelist.add("News");
		query2.field("ifmId").in(ifmIds);
		query2.field("articleType").notIn(typelist);
		AggregationPipeline line = mongodb
				.getDatastore()
				.createAggregation(entity)
				.match(query2)
				.group(Group.id(Group.grouping("groupId")),
						Group.grouping("reprintNumb",
								new Accumulator("$sum", 1)))
				.sort(Sort.ascending("reprintNumb")).limit(300000);
		Iterator<?> ator = line.aggregate(entity, AggregationOptions.builder()
				.allowDiskUse(true).build());
		while (ator.hasNext()) {
			MongodbDate date = (MongodbDate) ator.next();
			if (date.getReprintNumb() < 10) {
				datelist.add(date);
			}
		}
		// 聚合
		AggregationPipeline pipeline = mongodb
				.getDatastore()
				.createAggregation(entity)
				.match(query)
				.group(Group.id(Group.grouping("groupId")),
						Group.grouping("ifmId", Group.first("ifmId")),
						Group.grouping("articleId", Group.first("articleId")),
						Group.grouping("reprintNumb",
								new Accumulator("$sum", 1)))
				.sort(Sort.ascending("reprintNumb")).skip(datelist.size())
				.sort(Sort.descending("reprintNumb"));

		Iterator<?> tor = pipeline.aggregate(entity, AggregationOptions
				.builder().allowDiskUse(true).build());
		while (tor.hasNext()) {
			MongodbDate date = (MongodbDate) tor.next();
			Query<?> query3 = mongodb.getDatastore().createQuery(entity);
			query3.field("ifmId").equal(date.getIfmId());
			Iterator<?> iterator = query3.field("articleId")
					.equal(date.getArticleId()).iterator();
			while (iterator.hasNext()) {
				MongodbDate mdate = (MongodbDate) iterator.next();
				mdate.setContent("");
				mdate.setReprintNumb(date.getReprintNumb());
				if (mdate.getArticleType().equals("News")
						|| mdate.getReprintNumb() >= greaterForward
						&& mdate.getReprintNumb() < lessForward) {
					list.add(mdate);
				}
			}
		}
		if (list.size() > ((pageIndex - 1) * pageSize)) {
			List<MongodbDate> listdate = list.subList(pageSize
					* (pageIndex - 1),
					((pageIndex * pageSize) > list.size() ? list.size()
							: (pageIndex * pageSize)));
			return listdate;
		} else {
			return null;
		}
	}*/


	/**
	 * 查询相似数据
	 * 
	 * @param entity
	 * @param groupId
	 * @param ifmIds
	 */
	public List<MongodbDate> findSimilarity(Class<?> entity, String groupId,
			List<String> ifmIds, List<String> idlist, String articleType2,Date endTime1,
			Date startTime1,String ifmId) {
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		Query<?> query = mongodb.getDatastore().createQuery(entity);
		query.field("groupId").equal(groupId);
		if (ifmIds != null) {
			query.field("ifmId").in(ifmIds);
		}
		if (StringUtils.isNotBlank(ifmId)) {
			query.field("ifmId").equal(ifmId);
		}
		if (null != endTime1 && null != startTime1) {
			//给结束时间加一天
			Calendar ca = Calendar.getInstance();
			ca.setTime(endTime1);
			ca.add(Calendar.DATE, 1);
			endTime1 = ca.getTime();
			query.field("releaseTime").lessThan(endTime1);
			query.field("releaseTime").greaterThanOrEq(startTime1);
		}
		if (idlist.size()>0 && idlist != null) {
				query.field("articleType").in(idlist);
		}
		if (StringUtils.isNotBlank(articleType2)) {
			if (articleType2.equals(Menu.News.name())) {
				query.field("articleType").equal(articleType2);
			}
		}
		Iterator<?> tor = query.order(Sort.ascending("releaseTime")).iterator();
		while (tor.hasNext()) {
			MongodbDate date = (MongodbDate) tor.next();
			date.setContent(null);
			list.add(date);
		}
		return list;
	}

	/*public static void main(String[] args) {
		MongodbWarning mongodb = new MongodbWarning();
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		List<String> ifmIds = new ArrayList<>();
		// ifmIds.add("234");
		ifmIds.add("303");
		// ifmIds.add("290");
		PageView pageView = new PageView();
		pageView.setPageNow(4);
		pageView.setPageSize(10);
		long l = System.currentTimeMillis();
		Date endTime = new Date(l);
		// LASTDATE.add(Calendar.MONTH, -1);
		// Date startTime = new Date(LASTDATE.getTime().getTime());
		String articleType = "News";
		int greaterForward = 10;
		int lessForward = 20;

		list = mongodb.findHighRiskMore(MongodbDate.class, pageView, ifmIds,
				greaterForward, lessForward);
		// list = mongodb.findSimilarity(MongodbDate.class,
		// "9ccac8548d7aeaf3e726570a9889e343", ifmIds);
		System.out.println(list.size());
		for (MongodbDate mlist : list) {
			System.out.println(mlist);
		}
	}*/
}
